/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2018/9/9 11:04
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.security;

import com.hyacinth.constant.IDict;
import com.hyacinth.vo.ResourceVo;
import com.hyacinth.entity.User;
import com.hyacinth.service.ResourceService;
import com.hyacinth.service.RoleService;
import com.hyacinth.service.UserService;
import com.hyacinth.util.XaUtils;
import com.hyacinth.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@Slf4j
@Component
public class UrlCache {

    private final UserService userService;
    private final RoleService roleService;
    private final ResourceService resourceService;

    private HashMap<String, Set<String>> urlRoles;
    private String[] roles;

    @Autowired
    public UrlCache(UserService userService,
                    RoleService roleService,
                    ResourceService resourceService) {
        this.userService = userService;
        this.roleService = roleService;
        this.resourceService = resourceService;
    }

    /**
     * 加载资源角色集合
     */
    public void loadUrlRoles(){
        log.info("<<<<<<<<<<<<<<<----初始化资源角色---->>>>>>>>>>>>>>>");
        // 查询所有url
        List<ResourceVo> resVoList = resourceService.findAllRes();
        urlRoles = new HashMap<>(resVoList.size());
        // 查询正常状态的url
        List<RoleVo> roleVos = roleService.findRoleAndResource();
        roles = new String[roleVos.size()];
        if (!XaUtils.isEmpty(roleVos)) {
            urlRoles = new HashMap<>(roleVos.size());
        } else {
            log.warn("加载不到任何资源信息！");
        }
    }

    /**
     * 获得当前登录用户
     */
    public User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(null == authentication) {
            return null;
        }
        Object userDetails = authentication.getPrincipal();
        String userAccount="";
        if(userDetails instanceof String){
            userAccount= (String)userDetails;
        }else if(userDetails instanceof SecurityUser){
            userAccount = ((SecurityUser)userDetails).getUsername();
        }
        User user = null;
        if (XaUtils.isNotEmpty(userAccount)){
            // 根据用户名获取用户
            user = userService.findByUserName(userAccount, IDict.dataFlag_OK);
        }
        return user;
    }

    public HashMap<String, Set<String>> getUrlRoles() {
        return urlRoles;
    }

    public String[] getRoles() {
        return roles;
    }
}
